package com.mo.lib.ui.dialog

import android.R
import android.app.Dialog
import android.graphics.Color
import android.os.Bundle
import android.graphics.drawable.ColorDrawable
import android.view.*
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import java.lang.Exception

/**
 * @ author：mo
 * @ data：2021/3/29:9:21
 * @ 功能：
 */
abstract class BaseDialogFragment : DialogFragment() {
    /** 宽  */
    private var DEFAULT_WIDTH = WindowManager.LayoutParams.MATCH_PARENT

    /** 高  */
    private var DEFAULT_HEIGHT = WindowManager.LayoutParams.WRAP_CONTENT

    /** 位置  */
    private var DEFAULT_GRAVITY = Gravity.CENTER

    /** 默认可取消  */
    private var mCancelable = true

    /** 默认点击外部可取消  */
    private var mCanceledOnTouchOutside = true
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val mView = inflater.inflate(layoutId, container, false)
        doWhat(mView)
        return mView
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
//        val mDialog = super.onCreateDialog(savedInstanceState) //初始化
//        if (mDialog != null) {
//            mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
//            mDialog.setCanceledOnTouchOutside(mCanceledOnTouchOutside)
//            mDialog.setCancelable(mCancelable)
//            val window = mDialog.window
//            if (null != window) {
//                window.decorView.setPadding(0, 0, 0, 0)
//                window.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
//                val lp = window.attributes
//                lp.width = DEFAULT_WIDTH
//                lp.height = DEFAULT_HEIGHT
//                lp.gravity = DEFAULT_GRAVITY
//                lp.windowAnimations = R.style.Animation_InputMethod
//                window.attributes = lp
//            }
//            mDialog.setOnKeyListener { dialog, keyCode, event -> !mCancelable }
//        }
        return super.onCreateDialog(savedInstanceState).apply {
            this.requestWindowFeature(Window.FEATURE_NO_TITLE)
            this.setCanceledOnTouchOutside(mCanceledOnTouchOutside)
            this.setCancelable(mCancelable)
            val window = this.window
            if (null != window) {
                window.decorView.setPadding(0, 0, 0, 0)
                window.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
                val lp = window.attributes
                lp.width = DEFAULT_WIDTH
                lp.height = DEFAULT_HEIGHT
                lp.gravity = DEFAULT_GRAVITY
                lp.windowAnimations = R.style.Animation_InputMethod
                window.attributes = lp
            }
            this.setOnKeyListener { dialog, keyCode, event -> !mCancelable }
        }
    }

    /** 设置位置  */
    fun setGravity(gravity: Int) {
        DEFAULT_GRAVITY = gravity
    }

    /** 设置宽  */
    fun setWidth(width: Int) {
        DEFAULT_WIDTH = width
    }

    /** 设置高  */
    fun setHeight(height: Int) {
        DEFAULT_HEIGHT = height
    }

    /** 设置点击返回按钮是否可取消  */
    override fun setCancelable(cancelable: Boolean) {
        mCancelable = cancelable
    }

    /** 设置点击外部是否可取消  */
    fun setCanceledOnTouchOutside(canceledOnTouchOutside: Boolean) {
        mCanceledOnTouchOutside = canceledOnTouchOutside
    }

    /** 设置布局  */
    protected abstract val layoutId: Int

    /** 初始化Views  */
    protected abstract fun doWhat(view: View?)
    fun show(manager: FragmentManager) {
        if (this.isAdded) return
        try {
            val c = Class.forName("androidx.fragment.app.DialogFragment")
            val dismissed = c.getDeclaredField("mDismissed")
            dismissed.isAccessible = true
            dismissed[this] = false
            val shownByMe = c.getDeclaredField("mShownByMe")
            shownByMe.isAccessible = true
            shownByMe[this] = true
            val fragmentTransaction = manager.beginTransaction()
            fragmentTransaction.add(this, fragmentTag)
            fragmentTransaction.commitAllowingStateLoss()
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    private val fragmentTag: String
        private get() = "aa"
}